package com.filmatchs.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient; //
//import org.json.JSONArray;
//import org.json.JSONException;
//import org.json.JSONObject;

import android.util.Log;

/**
 * 
 * @author dycode (khalifavi)
 * 
 */
public class HttpClientManager {

	public static final String TAG = "RestClient";

	public static final int METHOD_GET = 0;
	public static final int METHOD_POST = 1;
	public static final int METHOD_PUT = 2;
	public static final int METHOD_DELETE = 4;

	//private String postfix = "";
	private String result = null;

	public HttpClientManager(String url) throws IOException,
			ClientProtocolException {
		this.result = connectResponse(url);
	}

	public HttpClientManager(String url, int method) throws IOException,
			ClientProtocolException {
		this.result = connectResponse(url, method);
	}

	public HttpClientManager(String url, int method, String postfix)
			throws IOException, ClientProtocolException {
		this.result = connectResponse(url, method, postfix);
	}

	public String getResult() {
		return this.result;
	}

	private static String convertStreamToString(InputStream is) {

		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	/**
	 * 
	 * @param url
	 *            URL untuk melakukan GET dengan mengembalikan JSON String
	 * @return String pengembalian nilai dari HTTP Request
	 */
	public static String connectResponse(String url) throws IOException,
			ClientProtocolException {
		return connectResponse(url, METHOD_GET, null);
	}

	/**
	 * 
	 * @param url
	 *            untuk mengambil JSON
	 * @param method
	 *            untuk dilakukan (GET, POST, PUT, DELETE)
	 * @return String pengembalian nilai dari HTTP Request
	 */
	public static String connectResponse(String url, int method)
			throws IOException, ClientProtocolException {
		return connectResponse(url, method, null);
	}

	/**
	 * 
	 * @param url
	 *            alamat URL
	 * @param method
	 *            untuk dilakukan (GET, POST, PUT, DELETE)
	 * @param returnType
	 *            tipe dari pengembalian (JSON, XML)
	 * @return String pengembalian nilai dari HTTP Request
	 */
	public static String connectResponse(String url, int method, String postfix)
			throws IOException, ClientProtocolException {
		HttpClient httpClient = new DefaultHttpClient();

		if (postfix == null) {
			postfix = "";
		}

		String retval = "";

		url += postfix;

		HttpResponse response = null;

		if (method == METHOD_POST) {
			// -------------------------------- if POST
			response = httpClient.execute(new HttpPost(url));
		} else if (method == METHOD_PUT) {
			// -------------------------------- if PUT
			response = httpClient.execute(new HttpPut(url));
		} else if (method == METHOD_DELETE) {
			// -------------------------------- if DELETE
			response = httpClient.execute(new HttpDelete(url));
		} else {
			// -------------------------------- default GET
			response = httpClient.execute(new HttpGet(url));
		}

		Log.i(TAG, response.getStatusLine().toString());

		HttpEntity entity = response.getEntity();

		if (entity != null) {

			InputStream instream = entity.getContent();
			String result = convertStreamToString(instream);
			retval = result;
			instream.close();
		}

		return retval;
	}

}
